import time
import unittest

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait


class TestSendMail(unittest.TestCase):
    def setUp(self):
        self.browser = webdriver.Chrome()
        self.browser.get('https://www.126.com/')
        self.result = ''
        self.target = '邮件发送成功'
        self.frame = self.browser.find_element_by_xpath(
            '/html/body/div[2]/div[3]/div[1]/div/div[4]/div[1]/div[1]/iframe')
        self.browser.switch_to.frame(self.frame)

        self.usrname = self.browser.find_element_by_name('email').send_keys('gench2110153')
        self.pwd = self.browser.find_element_by_name('password').send_keys('Qq1729256881')
        self.browser.find_element_by_id('dologin').click()
        WebDriverWait(self.browser, 60, 0.5).until(
            lambda browser: browser.find_element_by_xpath('/html/body/div[1]/nav/div[1]/ul/li[2]/span[2]'))
        self.browser.find_element_by_xpath('/html/body/div[1]/nav/div[1]/ul/li[2]/span[2]').click()

        WebDriverWait(self.browser, 10, 0.5).until(
            lambda browser: browser.find_element_by_xpath(
                '/html/body/div[2]/div[1]/div[2]/div[1]/section/header/div[1]/div[1]/div/div[2]/div/input'))
        subjtext = self.browser.find_element_by_xpath(
            '/html/body/div[2]/div[1]/div[2]/div[1]/section/header/div[1]/div[1]/div/div[2]/div/input')
        subjtext.send_keys('1729256881@qq.com')
        # subjtext.send_keys(Keys.ENTER)
        time.sleep(3)
        self.subj = self.browser.find_element_by_xpath(
            '/html/body/div[2]/div[1]/div[2]/div[1]/section/header/div[2]/div[1]/div/div/input')

        time.sleep(1)

    def test_sendmail01(self):
        # 填入主题
        self.subj.send_keys('Subj')
        # 填入内容
        self.contentframe = self.browser.find_element_by_class_name(
            'APP-editor-iframe')
        self.browser.switch_to.frame(self.contentframe)
        self.browser.find_element_by_xpath('/html/body/p').send_keys('Cont')
        self.browser.switch_to.default_content()
        self.browser.find_element_by_xpath('/html/body/div[2]/div[1]/div/div[1]/section/footer/div[1]/span[2]').click()
        time.sleep(2)
        self.result = self.browser.find_element_by_xpath(
            '/html/body/div[2]/div[1]/div/div[2]/section/h1').text
        self.assertEqual(self.result, self.target, '测试未通过')

    def test_sendmail02(self):
        # 填入主题
        self.subj.send_keys('')
        # 填入内容
        self.contentframe = self.browser.find_element_by_class_name(
            'APP-editor-iframe')
        self.browser.switch_to.frame(self.contentframe)
        self.browser.find_element_by_xpath('/html/body/p').send_keys('Cont')
        self.browser.switch_to.default_content()
        self.browser.find_element_by_xpath('/html/body/div[2]/div[1]/div/div[1]/section/footer/div[1]/span[2]').click()
        time.sleep(5)
        self.browser.find_element_by_xpath('/html/body/div[9]/div[3]/div[2]/div[1]').click()
        time.sleep(2)
        self.result = self.browser.find_element_by_xpath(
            '/html/body/div[2]/div[1]/div/div[2]/section/h1').text
        self.assertEqual(self.result, self.target, '测试未通过')

    def test_sendmail03(self):
        # 填入主题
        self.subj.send_keys('Subj')
        # 填入内容
        self.contentframe = self.browser.find_element_by_class_name(
            'APP-editor-iframe')
        self.browser.switch_to.frame(self.contentframe)
        self.browser.find_element_by_xpath('/html/body/p').send_keys('')
        self.browser.switch_to.default_content()
        self.browser.find_element_by_xpath('/html/body/div[2]/div[1]/div/div[1]/section/footer/div[1]/span[2]').click()
        time.sleep(10)
        self.result = self.browser.find_element_by_xpath(
            '/html/body/div[2]/div[1]/div/div[2]/section/h1').text
        self.assertEqual(self.result, self.target, '测试未通过')

    def test_sendmail04(self):
        # 填入主题
        self.subj.send_keys('')
        # 填入内容
        self.contentframe = self.browser.find_element_by_class_name(
            'APP-editor-iframe')
        self.browser.switch_to.frame(self.contentframe)
        self.browser.find_element_by_xpath('/html/body/p').send_keys('')
        self.browser.switch_to.default_content()
        self.browser.find_element_by_xpath('/html/body/div[2]/div[1]/div/div[1]/section/footer/div[1]/span[2]').click()
        time.sleep(5)
        self.browser.find_element_by_xpath('/html/body/div[9]/div[3]/div[2]/div[1]').click()
        time.sleep(2)
        self.result = self.browser.find_element_by_xpath(
            '/html/body/div[2]/div[1]/div/div[2]/section/h1').text
        self.assertEqual(self.result, self.target, '测试未通过')

    def tearDown(self):
        self.browser.close()


if __name__ == '__main__':
    unittest.main()
